<template>
  <div>

    <div class="loan-display">

      <div class="loan-item">
        <span class="label">信用卡：</span>
        <span class="label" v-for="(item, index) in list.xinyongka" :key="index"> <el-tag style="font-size: 22px;"
            effect="plain" type="danger" size="large">{{ item }}</el-tag></span>
      </div>

      <div class="loan-item">
        <span class="label">网贷：</span>
        <span class="label" v-for="(item, index) in list.wangdai" :key="index"> <el-tag style="font-size: 22px;"
            effect="plain" size="large">{{ item }}</el-tag></span>
      </div>

      <div class="loan-item">
        <span class="label">信用贷：</span>
        <span class="label" v-for="(item, index) in list.xinyongdai" :key="index"> <el-tag style="font-size: 22px;"
            effect="plain" type="warning" size="large">{{ item }}</el-tag></span>
      </div>
    </div>


    <div style="display: flex;">
      <div style="width: 900px;">
        <div style="display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px;border: 1px solid black;">
          <div style="border-right: 1px solid black; text-align: center; padding: 10px; background-color: #f9f9f9;">
            接单日期: <span class="c-#897777" style="margin-left: 15px;">{{ sp(answerRecord.orderMain.orderReceiveDate)
              }}</span>
          </div>
          <div style="border-right: 1px solid black; text-align: center; padding: 10px; background-color: #f9f9f9;">
            客户姓名: <span class="c-#897777" style="margin-left: 15px;">{{ answerRecord.orderMain.customName }}</span>
          </div>
          <div style="text-align: center; padding: 10px; background-color: #f9f9f9;">
            性别: <span class="c-#897777" style="margin-left: 15px;">{{ answerRecord.orderMain.sex }}</span>
          </div>
        </div>
        <div class="notes" style="border: 1px solid;"
          :style="{ backgroundColor: backgroundColor, fontSize: fontSize + 'px' }">
          <p v-html="formattedText"></p>
        </div>
        <div style="text-align: center;border: 1px solid;">
          <el-table :data="answerRecord.answerRecordList" border stripe>
            <el-table-column prop="answerDate" label="日期" align="center" column-key="date" width="210">
              <template #default="scope">
                <el-date-picker type="date" v-model="scope.row.answerDate" size="20" value-format="YYYY-MM-DD"
                  placeholder="接听日期" style="width: 165px; text-align: center;font-size: 20px;height: 40px;" />
              </template>
            </el-table-column>
            <el-table-column prop="answerRecord" align="center" label="接听平台电话记录" :header-cell-class-name="headerClass">
              <template #default="scope">
                <el-input maxlength="50" input-style="text-align: center;font-size: 20px;"
                  v-model="scope.row.answerRecord" autosize type="textarea" placeholder=""></el-input>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
      <div>
        <div style="padding: 10px; background-color: #f9f9f9;border: 1px solid;">
          接管手机号:<span class="c-#897777"><span style="margin-left: 15px;">{{ answerRecord.orderMain.transferPhoneCode
              }}</span></span>
        </div>
        <div style="border: 1px solid;">
          <el-table :data="answerRecord.phoneChangeRecords" border stripe>
            <el-table-column prop="changeDate" align="center" label="更换日期" width="110">
            </el-table-column>
            <el-table-column prop="phoneCode" align="center" label="更换手机号" width="100">
            </el-table-column>
            <el-table-column prop="phoneNo" align="center" label="手机号码" width="120">
            </el-table-column>
          </el-table>
        </div>

      </div>

    </div>
  </div>
</template>

<script>
import request from "@/config/axios";
import { ElMessage } from "element-plus";

export default {
  props: ['orderId','filter'],
  data() {
    return {
      loading: false,
      backgroundColor: '',
      fontSize: '',
      text: '',
      filters: [],
      list: {
        wangdai: [],
        xinyongdai: [],
        xinyongka: []
      },
      newRecord: {
        date: '',
        platform: '',
        details: ''
      },
      answerRecord: {
        orderMain: {
          orderReceiveDate: '123'
        },
        phoneChangeRecords: [],
        answerRecordList: []
      }
    };
  },
  computed: {
    formattedText() {
      // 将换行符 \n 替换为 <br>
      return this.text.replace(/\n/g, '<br>');
    },
  },
  watch: {
    orderId(newValue, oldValue) {
      //订单id发生变化查询新的信息
      this.getAnswerRecord(newValue)
    }
  },
  mounted() {
    this.getAnswerRecord(this.orderId)
    this.fetchData()
    this.getFont()
  },
  methods: {
    getFont() {
      request.get({
        url: '/answerTemplate/getLatestOne',
      }).then(res => {
        console.log(res);
        this.text = res.text
        this.backgroundColor = res.backgroundColor
        this.fontSize = res.fontSize
      })
    },
    async deleteItem(row) {

      ElMessageBox.confirm('真的要删除吗！', '删除 ' + `${row.answerDate} 的所有数据吗？`, {
        confirmButtonText: 'OK',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          request.delete({
            url: '/answerRecord/deleteById',
            params: { id: row.id }
          }).then(() => {
            this.getAnswerRecord(this.orderId)
          })
          ElMessage.success('删除成功')
        })
        .catch(() => {
          ElMessage({
            type: 'info',
            message: '已取消退出',
          });
        });
    },
    headerClass() {
      return 'custom-header-class'; // 返回自定义的类名
    },
    sp(row) {
      if (row) {
        return row.split(' ')[0]
      }
    },
    addRow() {
      this.answerRecord.answerRecordList.push({ "orderId": this.orderId, answerDate: '', answerRecord: '' }); // 添加新的一行
    },
    filterHandler(value, row, column) {
      const property = column['property']
      return row[property] === value
    },
    async getAnswerRecord(orderId) {
      const response = await request.get({
        url: '/answerRecord/getDetailByOrderId',
        params: { "orderId": orderId,...this.filter  }
      })
      this.answerRecord = response
      console.log('---response---', response)
    },
    fetchData() {
      request.get({
        url: '/trusteeshipDetail/listAllByOrderId',
        params: { "orderId": this.orderId,}
      }).then(res => {
        const response = res
        this.list = {
          wangdai: [],
          xinyongdai: [],
          xinyongka: []
        }
        if (response.length > 0) {
          response.forEach(item => {
            if (item.platformType === '网贷') {
              this.list.wangdai.push(item.platform)
            }
            if (item.platformType === "信用贷") {
              this.list.xinyongdai.push(item.platform)
            }
            if (item.platformType === "信用卡") {
              this.list.xinyongka.push(item.platform)
            }
          })
        }
      })





    },
    async updateAllHandle() {
      console.log(this.answerRecord.answerRecordList, "触发了保存操作")
      const hasEmptyFields = this.answerRecord.answerRecordList.some(item =>
        !item.answerDate || !item.answerRecord
      );

      if (this.answerRecord.answerRecordList.length > 0) {
        if (hasEmptyFields) {
          ElMessage.error('有空白数据不能保存，认真点 ！');
          return;
        }

        try {
          this.loading = true;
          await request.post({
            url: '/answerRecord/updateAll',
            data: this.answerRecord.answerRecordList
          });
          ElMessage.success('保存成功');
          this.$emit('close');
        } catch (error) {
          console.error(error);
        }
        finally {
          this.loading = false;
        }
      }

    }
  }
};
</script>

<style scoped>
.table {
  width: 100%;
  border-collapse: collapse;
}

table {
  width: 100%;
  margin-bottom: 20px;
}

th,
td {
  border: 1px solid #ccc;
  text-align: center;
}

.notes {
  font-size: 15px;
  background: #ffd9d9;
  padding: 5px;
  font-weight: 800;
}

.add-record {
  margin-top: 10px;
}

.el-table .cell {
  text-align: center;
  /* 使单元格文字居中 */
}

.el-textarea .el-textarea__inner {
  text-align: center !important
}

.custom-header-class {
  color: #fff;
  /* 字体颜色 */
  background-color: #409EFF;
  /* 背景颜色 */
  font-weight: bold;
  /* 加粗字体 */
  text-align: center;
  /* 居中对齐 */
  /* 可以根据需要添加更多样式 */
}

.loan-display {
  display: flex;
  flex-direction: column;

}

.loan-item {
  margin-bottom: 8px;
  font-size: 16px;
}

.label {
  font-weight: bold;
  margin-right: 8px;
}

.value {
  color: #333;
}
</style>
